<?php

namespace Data;

class UserCondition extends Base {

	public function saveCondition($userId, $city, $age, $sex, $weight, $length, $height, $color, $breedIds) {
		$where = array('UserId' => $userId);
		$result = $this->select('ConditionId')->where($where)->findOne();
		$data = array(
			'UserId' => $userId,
			'City' => $city,
			'Age' => $age,
			'Sex' => $sex,
			'Weight' => $weight,
			'Length' => $length,
			'height' => $height,
			'Color' => $color,
			'Breeds' => implode(',', $breedIds),
		);
		if ($result) {
			$data['UpdateTime'] = time();
			$this->where($where)->update($data);
		} else {
			$data['CreateTime'] = time();
			$data['UpdateTime'] = time();
			$this->insert($data);
		}
	}

	public function getUserConditionByUserId($userId) {
		$where = array('UserId' => $userId);
		$result = $this->where($where)->findOne();
		if (!$result) {
			$data = array(
				'UserId' => $userId,
				'CreateTime' => time(),
				'UpdateTime' => time(),
			);
			$this->insert($data);
			$result = $this->where($where)->findOne();
		}
		return $result;
	}
}